<template>
  <view>
    <text>Hello, 模板</text>

    <!-- NutUI 组件按需加载示例 -->
    <view class="nutui-demo">
      <nut-button type="primary" @click="handleClick">NutUI 按钮</nut-button>

      <nut-cell-group title="基础用法">
        <nut-cell title="我是标题" desc="描述文字"></nut-cell>
        <nut-cell title="我是标题" desc="描述文字" is-link></nut-cell>
      </nut-cell-group>
    </view>
  </view>
</template>

<script setup lang="ts">
import Taro from '@tarojs/taro'
import { calcNavBarInfo } from '@/utils/utils'
import { onMounted, ref } from 'vue'
import { reactive } from 'vue'
import request from '@/utils/request'

const state = reactive({
  statusBarHeight: 0,
  token: null,
  showDialog: false,
  userInfo: {},
})

// NutUI 方法
const handleClick = () => {
  console.log('NutUI 按钮被点击了')
  Taro.showToast({
    title: 'NutUI 按钮点击成功',
    icon: 'success',
  })
}

// 处理开始答题的逻辑
const startQuiz = async () => {
  const loginInfo = await request('login', {
    token: state.token,
  })
  state.userInfo = loginInfo.data
  Taro.setStorageSync('userInfo', loginInfo.data)
  // 页面挂载时的逻辑
  if (!loginInfo.data.nick) {
    state.showDialog = true
  } else {
    Taro.navigateTo({
      url: '/subPackages/question/pages/index/index',
    })
  }
}
const handleData = async (data) => {
  const auth = await request('auth', {
    headImg: 'https://deep-mall-1254206734.cos.ap-guangzhou.myqcloud.com/bg1.png',
    nick: data.nickname,
    token: state.token,
  })

  state.showDialog = false
  Taro.navigateTo({
    url: '/subPackages/question/pages/index/index',
  })
}
const generateShenZhuIdentifier = (): string => {
  // 生成一个 5 位数的随机数字
  const randomNum = Math.floor(10000 + Math.random() * 90000)

  // 返回拼接结果
  return `畲族${randomNum}`
}
const introduceStart = () => {
  console.log('开始答题')
  Taro.navigateTo({
    url: '/subPackages/introduce/pages/index/index',
  })
}
const init = async () => {
  const { statusBarHeight } = await calcNavBarInfo()
  state.statusBarHeight = statusBarHeight as number
}
const initLoginInfo = () => {
  Taro.login({
    success: async (res) => {
      const result = await request('getCode', { code: res.code })
      state.token = result.data.token
      Taro.setStorageSync('token', result.data.token)
    },
  })
}

onMounted(() => {
  console.log(1)
  init()
  initLoginInfo()
})
</script>

<style lang="scss">
@import './index.scss';

.nutui-demo {
  padding: 20px;

  .nut-button {
    margin-bottom: 10px;
  }

  .nut-cell-group {
    margin-bottom: 20px;
  }
}
</style>
